﻿//==================================================================
// Copyright (C) 2011  BPM
// 作 者：朱云
// 日 期：2011/12/21
// 描 述：表单用户控件
// 版 本：1.00
// 
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容     
//    
//==================================================================
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.BaseWebCtrlLib;
using System.Data;
using HF.WorkFlow.Const;
using HF.Business.Finance;
using HF.Business.Common;
using HF.BaseWebCtrlLib;
using HF.Framework.DataClientDBAgent;
using HF.Framework.SqlDataContract;
using HF.Business.CrmTeachSumup;
public partial class UserflowControl : BaseUserControl
{
    DataTable UserflowTable;
    string SnEvectionDet = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load();
        SnEvectionDet = WorkflowInsId;//

        if (CtrlState == WorkFlowConst.STATE_VIEW)//控制编辑区是否显示
        {

            Panel2.Visible = false;
        }
        else
        {

            Panel2.Visible = true;
        }
        if (!IsPostBack)
        {
            initData();

        }
        UserflowTable = (DataTable)Session[SnEvectionDet];
        getCustomerName();
    }

    private void initData()
    {
        DataTable CrmTeachSumupTable = Userflow.GetCrmTeachSumupTable(WorkflowInsId);
        if (CrmTeachSumupTable != null && CrmTeachSumupTable.Rows.Count > 0)//检查是否有数据
        {
            Userflow sr = new Userflow();
            sr.GetCrmTeachSumupInfo(WorkflowInsId);
            tbxBillCode.Text = sr.Code;
            tbxRemark.Text = sr.EXP1;
            UserflowTable = EvectionDet.Getfin_EvectionDetTable(WorkflowInsId);
        }
        else
        {
            string billType = "FCCB";
            tbxBillCode.Text = billType + BusinessComm.GetBillCode(billType);
            ClientDBAgent agent = new ClientDBAgent();
            UserflowTable = agent.GetBlankTable("fin_EvectionDet");
        }
        Session.Add(SnEvectionDet, UserflowTable);
    }
    public override void SaveUserControl(bool IsDraft)
    {
        base.SaveUserControl(IsDraft);
        SqlDataItem sqlItem1 = new SqlDataItem();
        SqlDataItem sqlDataItem = new SqlDataItem();
        string sql = "delete CrmTeachSumup where WorkflowinsId=@WorkflowInsId";//先删除原有数据
        sqlItem1.CommandText = sql;
        sqlItem1.AppendParameter("@WorkflowInsId", WorkflowInsId);
        sql = "insert CrmTeachSumup(ID,Code,PrelectDate,IsBargain,IsStandard,IsValue,IsGripe,GripeExplain,IsClass,ClassExplain,TeachPlace,CustomerPromis,Explain," +
        "WorkflowId,WorkflowInsId,WorktaskId,WorktaskInsId)" +
            " values(@ID,@Code,@PrelectDate,@IsBargain,@IsStandard,@IsValue,@IsGripe,@GripeExplain,@IsClass,@ClassExplain,@TeachPlace,@CustomerPromis,@Explain," +
            "@WorkflowId,@WorkflowInsId,@WorktaskId,@WorktaskInsId)";
        System.Guid guid=Guid.NewGuid();
        sqlDataItem.CommandText = sql;
        sqlDataItem.ParameterList.Clear(); ;
        sqlDataItem.AppendParameter("@ID",guid.ToString());
        sqlDataItem.AppendParameter("@Code", tbxBillCode.Text);
        sqlDataItem.AppendParameter("@PrelectDate", YYSubmitDate.Text);
        sqlDataItem.AppendParameter("@IsBargain", RadioOpinion(radioIsyes.Checked));
        sqlDataItem.AppendParameter("@IsStandard", RadioOpinion(radioStandardyes.Checked));
        sqlDataItem.AppendParameter("@IsValue", RadioOpinion(radioYes.Checked));
        sqlDataItem.AppendParameter("@IsGripe", RadioOpinion(radioGripeyes.Checked));
        sqlDataItem.AppendParameter("@GripeExplain", txtGripeExplain.Text);
        sqlDataItem.AppendParameter("@IsClass", RadioOpinion(radioIsClassyes.Checked));
        sqlDataItem.AppendParameter("@ClassExplain", txtClassExplain.Text);
        sqlDataItem.AppendParameter("@TeachPlace",txtTeachPlace.Text);
        sqlDataItem.AppendParameter("@CustomerPromis", txtCusPromis.Text);
        sqlDataItem.AppendParameter("@Explain", tbxRemark.Text);
        sqlDataItem.AppendParameter("@WorkflowId", WorkflowId);
        sqlDataItem.AppendParameter("@WorkflowInsId", WorkflowInsId);
        sqlDataItem.AppendParameter("@WorktaskId", WorktaskId);
        sqlDataItem.AppendParameter("@WorktaskInsId", WorktaskInsId);


        SqlDataItemArray sqlItemAry = new SqlDataItemArray();
        sqlItemAry.AppendSqlItem(sqlItem1);
        sqlItemAry.AppendSqlItem(sqlDataItem);

        ClientDBAgent agent = new ClientDBAgent();
        agent.ExecuteNonQueryArray(sqlItemAry);
        //base.SaveUserControl(IsDraft);
        //Userflow uf = new Userflow();
        //uf.WorkflowId = WorkflowId;
        //uf.WorkflowInsId = WorkflowInsId;
        //uf.WorktaskId = WorktaskId;
        //uf.WorktaskInsId = WorktaskInsId;
        //uf.ID = tbxBillCode.Text;
        //uf.EXP1 = tbxRemark.Text;

        //DataTable edt = Userflow.GetCrmTeachSumupTable(WorkflowInsId);
        //if (edt != null && edt.Rows.Count > 0)//检查是否有数据。
        //    uf.Update();
        //else
        //    uf.Insert();

    }
    public string RadioOpinion(bool radio)
    {
        if (radio)
        {
            return "1";
        }
        return "0";
    }
    public void getCustomerName()
    {
        DataTable dt = CrmCustomer.GetCrmCustomerName();
        ddlCrmCustomer.DataSource = dt;
        ddlCrmCustomer.DataTextField = "Code";
        ddlCrmCustomer.DataValueField = "ID";
        ddlCrmCustomer.DataBind();

    }
    protected void ddlCrmCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        string test = ddlCrmCustomer.SelectedValue;
        DataTable dt = CrmCustomer.GetCrmCustomerTable(ddlCrmCustomer.SelectedItem.Text.Trim());
        txtAddress.Text = dt.Rows[0]["Address"].ToString();


    }

}